#include<cstdio>
using namespace std;
char a[26],ch,id[26],ser[1001][26];
int sz[26],le[1001],bh[1001],bzzz[1001];
int main()
{
	freopen("network.in","r",stdin);
	freopen("network.out","w",stdout);
	int n,len=0,z,ns=0;
	scanf("%d",&n);
	ch=getchar();
	for(int i=1;i<=n;i++)
	{
		len=0;
		while(ch<'A'||ch>'z')
			ch=getchar();
		while(ch>='A'&&ch<='z')
		{
			id[++len]=ch;
			ch=getchar();
		}
		z=1;
		if(id[1]=='C')
			z=2;
		len=0;
		while(ch!='.'&&ch!=':'&&(ch<'0'||ch>'9'))
			ch=getchar();
		while(ch=='.'||ch==':'||(ch>='0'&&ch<='9'))
		{
			id[++len]=ch;
			ch=getchar();
		}
		int gs=0,gs1=1,bz=0;
		sz[1]=0;
		bzzz[1]=bzzz[2]=bzzz[3]=bzzz[4]=bzzz[5]=0;
		for(int j=1;j<=len;j++)
		{
			if(id[j]=='.'||id[j]==':')
			{
				a[++gs]=id[j];
				gs1++;
				sz[gs1]=0;
			}
			else
			{
				sz[gs1]=sz[gs1]*10+id[j]-48;
				bzzz[gs1]=1;
				if(id[j]=='0'&&(id[j-1]=='.'||id[j-1]==':')&&(id[j+1]!='.'&&id[j+1]!=':'))
				{
					bz=1;
					break;
				}
			}
		}
		if(gs>4||bzzz[1]==0||bzzz[2]==0||bzzz[3]==0||bzzz[4]==0||bzzz[5]==0||a[1]!='.'||a[2]!='.'||a[3]!='.'||a[4]!=':'||gs1>5||sz[1]>255||sz[2]>255||sz[3]>255||sz[4]>255||sz[5]>65535||bz==1)
		{
			printf("ERR\n");
			continue;
		}
		if(z==1)
		{
			int bz=0;
			for(int k=1;k<=ns;k++)
			{
				if(le[k]!=len)
					continue;
				int bz1=0;
				for(int j=1;j<=len;j++)
					if(id[j]!=ser[k][j])
					{
						bz1=1;
						break;
					}
				if(bz1==0)
				{
					bz=1;
					break;
				}
			}
			if(bz==0)
			{
				printf("OK\n");
				ns++;
				for(int j=1;j<=len;j++)
					ser[ns][j]=id[j];
				le[ns]=len;
				bh[ns]=i;
			}
			else
				printf("FAIL\n");
		}
		else
		{
			int bz=0;
			for(int k=1;k<=ns;k++)
			{
				if(le[k]!=len)
					continue;
				int bz1=0;
				for(int j=1;j<=len;j++)
					if(id[j]!=ser[k][j])
					{
						bz1=1;
						break;
					}
				if(bz1==0)
				{
					bz=bh[k];
					break;
				}
			}
			if(bz==0)
				printf("FAIL\n");
			else
				printf("%d\n",bz);
		}
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}
